const getDistance = require("../../../utils/getCity.js")
Page({
  data: {
    jobList: [],
    clearShow: "hide",
    value: "",
    params: {
      aid: 3,
      pid: 1
    },
    showThing: "",
    benifit: [],
    timebox: [],
    distance: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  showValue: function (e) {
    var that = this;
    that.setData({
      value: e.detail.value
    })
  },
  focusInput: function (e) {
    var that = this;
    that.setData({
      clearShow: ""
    })
  },
  clearInput: function () {
    var that = this;
    that.setData({
      clearShow: "hide",
      value: "",
      showThing: "show",
      jobList: []
    })
  },
  searching: function () {
    var that = this;
    if (that.data.value == '') {
      wx.showToast({
        title: '请输入搜索内容',
        icon: "none"
      })
      return;
    }
    getApp().globalData.get("/api/part_time/search",{
      param: that.data.value
    }).then(res=>{
      const that = this;
      var box = res.data.content;
      for (let i in box) {
        let benifit = box[i].benifit.split(",");
        this.data.benifit.push(benifit)
        this.data.timebox.push(this.getTime(box[i].createdTime))
        getDistance.getDistance([{
          latitude: box[i].latitude,//39.915405, 116.403802
          longitude: box[i].longitude
        }]).then(res => {
          if (res < 1000) {
            that.data.distance.push(res + "m");
          } else {
            that.data.distance.push((res / 1000).toFixed(1) + "km");
          }
          that.setData({
            distance: that.data.distance,
          })
        })
      }
      that.setData({
        jobList: box,
        benifit: that.data.benifit,
        timebox: that.data.timebox,
        distance: that.data.distance,
        showThing: "show"
      })
    })
  },
  onReachBottom() {
    const that = this;
    that.data.params.pid = that.data.params.pid + 1;
    
  },
  onPullDownRefresh() {
    const that = this;
    that.data.params.pid = 1;
  },
  tomore(e) {
    var url = '/pages/recruit/details/details?partTimeId=' + e.currentTarget.dataset.parttimeid;
    wx.navigateTo({
      url: url,
    })
  },
  getTime: function (time) {
    var date1 = new Date(); //新建一个日期对象，默认现在的时间
    var date2 = new Date(time); //设置过去的一个时间点，"yyyy-MM-dd HH:mm:ss"格式化日期
    var date3 = date1 - date2;
    //计算出相差天数
    var days = Math.floor(date3 / (24 * 3600 * 1000))
    //计算出小时数
    var leave1 = date3 % (24 * 3600 * 1000)    //计算天数后剩余的毫秒数
    var hours = Math.floor(leave1 / (3600 * 1000))
    //计算相差分钟数
    var leave2 = leave1 % (3600 * 1000)        //计算小时数后剩余的毫秒数
    var minutes = Math.floor(leave2 / (60 * 1000))
    //计算相差秒数
    var leave3 = leave2 % (60 * 1000)      //计算分钟数后剩余的毫秒数
    var seconds = Math.round(leave3 / 1000)
    // console.log(days + "----" + hours + "----" + minutes + "----" + seconds)
    if (days > 0) {
      return days + "天前";
    } else if (hours > 0) {
      return hours + "小时前";
    } else if (minutes > 0) {
      return minutes + "分钟前";
    } else {
      return seconds + "秒前";
    }
  }
})